package com.trailwalker.inputmethod.activity;

import com.trailwalker.javafx.core.activity.Activity;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

/**
 * @author Trailwalker
 * @create 2021-10-15 16:59
 * @des
 */
public class ImageActivity extends Activity {
    @Override
    public void onCreate() {
        super.onCreate();
        AnchorPane apRoot = new AnchorPane();
        setContentView(apRoot);

        Image image = new Image(ResourceUtils.getImage(this, "avatar.jpg"), 200, 200, false, true);
        ImageView imageView = new ImageView(getBottomRadiusImage(image, 30));
        apRoot.getChildren().add(imageView);

        ImageView imageView2 = new ImageView(image);
        apRoot.getChildren().add(imageView2);
        AnchorPane.setTopAnchor(imageView2, 0.0);
        AnchorPane.setLeftAnchor(imageView2, 250.0);
    }

    public Image getBottomRadiusImage(Image image, int radius) {
        BufferedImage fxBi = SwingFXUtils.fromFXImage(image, null);
        BufferedImage bufferedImage = new BufferedImage((int) image.getWidth(), (int) image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = bufferedImage.createGraphics();

        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, -radius, (int) image.getWidth(), (int) image.getHeight() + radius, radius, radius));
        gs.drawImage(fxBi, 0, 0, null);
        gs.dispose();
        return SwingFXUtils.toFXImage(bufferedImage, null);
    }
}
